package algorithm_demo.demo01;

/**
 * 相同树
 * https://leetcode.cn/problems/same-tree/
 *
 * @author Api
 * @date 2023/2/4 13:27
 */
public class Code24_SameTree {
    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    public static boolean isSameTree(TreeNode p, TreeNode q){
        // 按位异或^：相同为0，不同为1，0为false， 1为true
        if (p == null ^ q == null){
            return false;
        }
        if (p == null && q == null){
            return true;
        }
        return p.val == q.val && isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
    }
}
